<template>
  <div class="app-container customer-div">
    <el-menu :default-active="listQuery.tab" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="my">我的</el-menu-item>
      <el-menu-item index="department" v-if="saleDeliverFeedbackDepartment">部门</el-menu-item>
      <el-menu-item index="all" v-if="saleDeliverFeedbackAll">全部</el-menu-item>
    </el-menu>
    <div style="margin-top: 20px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="130px">
        <el-form-item label="订单编号：">
          <el-input v-model="listQuery.orderSn" clearable></el-input>
        </el-form-item>
        <el-form-item label="客户编号：">
          <el-input v-model="listQuery.customerNumber" clearable></el-input>
        </el-form-item>
        <el-form-item label="客户名称：">
          <el-select
            clearable
            v-model="listQuery.customerName"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
              v-for="item in customerList"
              :key="item.customerName"
              :label="item.customerName"
              :value="item.customerName">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="牌号：">
          <el-select v-model="listQuery.kingGoodsId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in goodsList"
              :key="item.kingId"
              :label="item.goodsSn"
              :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销售员：">
          <el-select v-model="listQuery.userId" placeholder="请选择" clearable filterable>
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.realName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="延迟反馈：">
          <el-select v-model="listQuery.delayedFeedback" class="input-width" style="width:195px" placeholder="请选择" clearable multiple>
            <el-option value="反应激烈无协商余地" label="反应激烈无协商余地" :key="0"></el-option>
            <el-option value="有不满但可协商" label="有不满但可协商" :key="1"></el-option>
            <el-option value="可接受无不满" label="可接受无不满" :key="2"></el-option>
            <el-option value="未标记" label="未标记" :key="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预计发货日期：">
          <el-date-picker
            v-model="listQuery.sendDate"
            type="daterange"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="延迟天数：">
          <el-select v-model="listQuery.delayDays" placeholder="请选择" clearable>
            <el-option key="1" label="延迟1天" value="1"></el-option>
            <el-option key="2" label="延迟2天" value="2"></el-option>
            <el-option key="3" label="延迟3天" value="3"></el-option>
            <el-option key="4" label="延迟3天以上" value="4"></el-option>
            <el-option key="-1" label="提前1天" value="-1"></el-option>
            <el-option key="-2" label="提前2天" value="-2"></el-option>
            <el-option key="-3" label="提前3天" value="-3"></el-option>
            <el-option key="-4" label="提前3天以上" value="-4"></el-option>
          </el-select>

        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div>
      <el-radio-group v-model="listQuery.type" size="small" @input="changeType">
        <el-radio-button label="unMark">未标记</el-radio-button>
        <el-radio-button label="mark">已标记</el-radio-button>
        <el-radio-button label="send">已发货</el-radio-button>
      </el-radio-group>
    </div>
    <div style="font-size:14px;line-height: 30px;padding:10px 0 ">
      反应激烈无协商余地订单数量占比：<span style="font-weight: bold">{{num[0] ? num[0] : 0 }}%</span>
      <span style="padding-left:20px;">有不满但可协商订单数量占比：</span><span style="font-weight: bold">{{num[1] ? num[1] : 0 }}%</span>
      <span style="padding-left:20px;">可接受无不满订单数量占比：</span><span style="font-weight: bold">{{num[2] ? num[2] : 0 }}%</span>
    </div>
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="list"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column
          label="订单号"
          align="center"
          min-width="180">
          <template slot-scope="scope">
            {{scope.row.orderSn}}
          </template>
        </el-table-column>
        <el-table-column
          label="客户"
          align="center"
          min-width="180">
          <template slot-scope="scope">
            <div>
              {{scope.row.customerNumber}}
            </div>
            <!--是否有权限-->
            <div v-if="scope.row.isShowCustomerName">{{scope.row.customerName}}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="商品牌号"
          align="center"
          min-width="180">
          <template slot-scope="scope">
            {{scope.row.goodsSn}}
          </template>
        </el-table-column>
        <el-table-column
          label="合同数量"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsNum}}
          </template>
        </el-table-column>
        <el-table-column
          label="业务员"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>
        <el-table-column
          prop="sendDate"
          label="预计发货日期"
          align="center"
          min-width="150">
        </el-table-column>

        <el-table-column
          align="center"
          label="延迟天数"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.delayDays}}
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="延迟反馈"
          min-width="100">
          <template slot-scope="scope">
            {{scope.row.delayedFeedback}}
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="操作"
          fixed="right"
          min-width="100">
          <template slot-scope="scope">
              <el-button
                v-if="listQuery.tab=='my'"
                type="text"
                size="mini"
                @click="showEdit(scope.row)">{{ scope.row.delayedFeedback == '' ? '标记' : '修改' }}
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="showLog(scope.row)">日志
              </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

    <!--日志-->
    <el-dialog
      title="日志"
      :visible.sync="dialogVisible"
      width="600px">
          <div style="max-height: 500px;overflow-y: auto;padding-top: 10px">
            <el-timeline>
              <el-timeline-item
                v-for="(activity, index) in logList"
                :key="index"
                :timestamp="activity.logTime">
                {{activity.logContent}}
              </el-timeline-item>
            </el-timeline>
            <div v-if="logList.length == 0" style="color:#888;text-align: center">- 暂无数据 -</div>
          </div>
    </el-dialog>

    <!--标记-->
    <el-dialog
      title="标记"
      :visible.sync="dialogVisible2"
      width="400px">
      <el-form :model="form" size="mini" label-width="100px" ref="form" :rules="rules">
        <el-form-item label="延迟反馈：" prop="delayedFeedback">
          <el-select v-model="form.delayedFeedback" class="input-width" style="width:195px" placeholder="请选择">
            <el-option value="反应激烈无协商余地" label="反应激烈无协商余地" :key="0"></el-option>
            <el-option value="有不满但可协商" label="有不满但可协商" :key="1"></el-option>
            <el-option value="可接受无不满" label="可接受无不满" :key="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { orderGoodsList,getCustomerOption,getUserOption,getOrderStatList} from '@/api/partner'
import {getDelayedFeedback,setDelayedFeedback} from '@/api/order'
import {formatDate} from '@/utils/date';
import store from '@/store'
import { mapGetters } from 'vuex'
const defaultListQuery={
  "tab": "my", //my-我的，department-部门，all-全部
  "userId": "", //业务员ID
  "orderSn": "", //订单号
  "customerName": "",  //客户名称
  "customerNumber": "", //客户编码
  "kingGoodsId": "", //商品牌号
  "delayedFeedback": [], //延迟反馈（反应激烈无协商余地，有不满但可协商，可接受无不满）
  "sendDate": [], //预计发货日期
  "pageNum": 1,
  "pageSize": 10,
  delayDays:'',
  type:'unMark',

};
const defaultForm = {
  "ogId": "", //订单商品ID
  "delayedFeedback": "" //延迟反馈
}
export default {
  name: "saleReport",
  data(){
    return{
      roles:store.getters.roles ? store.getters.roles : [],
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      list:[],
      count:0,
      loading:false,
      goodsList:[],
      customerList:[],
      userOptionList:[],

      goodsClassList:[],
      dialogVisible:false,
      logList:[],
      dialogVisible2:false,
      btnLoading:false,
      rules:{
        delayedFeedback:[{required: true, trigger: 'change' ,message: '此项必填'}],
      },
      form:JSON.parse(JSON.stringify(defaultForm)),
      num:[]
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
    saleDeliverFeedbackDepartment() {
      if (this.roles.indexOf("saleDeliverFeedbackDepartment") != -1) {
        return true
      } else {
        return false
      }
    },
    saleDeliverFeedbackAll() {
      if (this.roles.indexOf("saleDeliverFeedbackAll") != -1) {
        return true
      } else {
        return false
      }
    },
  },
  created() {
    this.getListData();
    this.getUserOptionList()
    this.getgoodsList()
  },
  filters: {
    formatTime(time) {
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd')
    },
  },
  methods:{
    changeType(name){
      this.listQuery.type = name
      this.getListData()
    },
    handleClose(){
      this.dialogVisible2 = false
    },
    handleConfirm(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let data = JSON.parse(JSON.stringify(this.form));
          setDelayedFeedback(data).then((res)=>{
            this.btnLoading = false;
            this.handleClose();
            this.getListData();
          })
        }
      })
    },
    showEdit(item){
        this.form.ogId = item.ogId
        this.form.delayedFeedback = item.delayedFeedback
        this.dialogVisible2 = true
    },
    showLog(item){
      this.logList = item.logList
      this.dialogVisible = true
    },
    getgoodsList(){
      orderGoodsList().then((res)=>{
        this.goodsList = res.list
      })
    },


    //获取订单状态列表
    getOrderStatListData(){
      getOrderStatList().then((res)=>{
        this.orderStateList = res;
      })
    },

    /**
     * 切换选择状态
     */
    handleSelect(key, keyPath) {
      this.listQuery = JSON.parse(JSON.stringify(defaultListQuery))
      this.listQuery.tab = key;
      this.handleSearchList();
    },


    //获取员工下拉选项
    getUserOptionList(){
      getUserOption().then((res)=>{
        this.userOptionList = res;
      })
    },
    //查询获取客户列表
    remoteMethod(query) {
      if(query && query!= ''){
        this.loading = true;
        getCustomerOption({keywords:query}).then(response => {
          this.customerList = response;
          this.loading = false;
        }).catch(()=>{
          this.loading = false;
        });
      }
    },
    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getListData();
    },

    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getListData();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getListData();
    },

    getListData(){
      this.loading = true;
      getDelayedFeedback(this.listQuery).then((res)=>{
        this.list = res.list;
        this.count = res.count;
        this.num = res.num
        this.loading = false;
      }).catch(()=>{
        this.loading = false;
      })
    },

  },
}
</script>

<style scoped lang="scss">

</style>
